import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores'
//导入登录页面组件
import Login from '@/views/login/Login.vue'
//导入教师页面相关组件
import TeacherHome from '@/views/teacher/TeacherHome.vue'
import TeachingProject from '@/views/teacher/TeachingProject.vue'
import Course from "@/views/teacher/Course.vue"
import Paper from "@/views/teacher/Paper.vue"
import TeachingAward from "@/views/teacher/TeachingAward.vue"
import StudentCompetition from "@/views/teacher/StudentCompetition.vue"
//导入教秘页面相关组件
import SecretaryHome from '@/views/secretary/SecretaryHome.vue'
import CheckTeachingProject from '@/views/secretary/CheckTeachingProject.vue'
import CheckCourese from '@/views/secretary/CheckCourse.vue'
import CheckPaper from '@/views/secretary/CheckPaper.vue'
import CheckTeachingAward from '@/views/secretary/CheckTeachingAward.vue'
import CheckStudentCompetition from '@/views/secretary/CheckStudentCompetition.vue'
import AchievementQuery from '@/views/secretary/AchievementQuery.vue'
import Forget from '@/views/Forget/Forget.vue'

//createRouter 创建路由实例
//配置 history 模式
//1.history模式： createWebHistory  地址栏不带 #
//2.hash模式： createWebHashHistory 地址栏带 #

// vite中的环境变量 import.meta.env.BASE_URL 就是 vite.config.js中的 base 配置项
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    //登录页面路由配置
    { path: '/', redirect: '/login' },
    { path: '/login', component: Login },
    //忘记密码页面路由配置
    {path:'/forget',component:Forget},
    //教师页面路由配置
    { path: '/teacher-home', component: TeacherHome, children: [
      {path: 'teachingProject', component: TeachingProject, name: 'teachingProject'},
      {path: 'course', component: Course, name: 'course'},
      {path: 'paper', component: Paper, name: 'paper'},
      {path: 'teachingAward', component: TeachingAward, name: 'teachingAward'},
      {path: 'studentCompetition', component: StudentCompetition, name: 'studentCompetition'}
    ] },

    //教秘页面路由配置
    { path: '/secretary-home', component: SecretaryHome, children: [
      { path: 'checkTeachingProject', component: CheckTeachingProject, name: 'checkTeachingProject'},
      { path: 'checkCourse', component: CheckCourese, name: 'checkCourse'},
      { path: 'checkPaper', component: CheckPaper, name: 'checkPaper'},
      { path: 'checkTeachingAward', component: CheckTeachingAward, name: 'checkTeachingAward'},
      { path: 'checkStudentCompetition', component: CheckStudentCompetition, name: 'checkStudentCompetition'},
      { path: 'achievementQuery', component: AchievementQuery, name: 'achievementQuery'}
    ]}
  ]
})


//登录访问拦截 => 默认是直接方形的
//根据返回值决定，是放行还是拦截
//返回值：
//1.undefined / true 直接放行
//2.false 拦回from的地址页面
//3.具体路径 或 路径对象 拦截到对应的地址
// '/login'  { name: 'login }
router.beforeEach((to) => {
  // 如果没有token，且访问的是非登录页，拦截到登录，其他情况正常放行
  const useStore = useUserStore()
   
   if(!useStore.token && to.path != '/login'&&to.path!='/forget') return '/login'
})

export default router
